package com.security.demo.alipay.wap.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.*;
import com.alipay.api.request.*;
import com.security.core.properties.AliPayProperties;
import com.security.demo.alipay.wap.AliPayWapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * AliPayWapServiceImpl
 *
 * @author: Dark_Blue
 * @date: 2020/3/24 10:12
 */
@Service
public class AliPayWapServiceImpl implements AliPayWapService {

    @Autowired
    private AliPayProperties aliPayProperties;

    @Override
    public String createOrder(String orderNo, String orderName, String amount, String description) {
        AlipayClient alipayClient = new DefaultAlipayClient(aliPayProperties.getGatewayUrl(), aliPayProperties.getAppid(),
                aliPayProperties.getAppPrivateKey(), aliPayProperties.getFormate(), aliPayProperties.getCharset(), aliPayProperties.getAlipayPublicKey(), aliPayProperties.getSignType());
        AlipayTradeWapPayRequest aliPayRequest=new AlipayTradeWapPayRequest();
        //设置请求参数
        aliPayRequest.setReturnUrl(aliPayProperties.getReturnUrl());
        aliPayRequest.setNotifyUrl(aliPayProperties.getNotifyUrl());
        // 封装请求支付信息
        AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
        model.setOutTradeNo(orderNo);
        model.setSubject(orderName);
        model.setTotalAmount(amount);
        model.setBody(description);
        model.setTimeoutExpress("2m");
        model.setProductCode("QUICK_WAP_WAY");
        aliPayRequest.setBizModel(model);
        //请求
        String result = null;
        try {
            result = alipayClient.pageExecute(aliPayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String getOrderInfo(String orderNo, String tradeNo) {
        AlipayClient alipayClient = new DefaultAlipayClient(aliPayProperties.getGatewayUrl(), aliPayProperties.getAppid(),
                aliPayProperties.getAppPrivateKey(), aliPayProperties.getFormate(), aliPayProperties.getCharset(), aliPayProperties.getAlipayPublicKey(), aliPayProperties.getSignType());
        AlipayTradeQueryRequest aliPayRequest = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model=new AlipayTradeQueryModel();
        model.setOutTradeNo(orderNo);
        model.setTradeNo(tradeNo);
        aliPayRequest.setBizModel(model);

        //请求
        String result = null;
        try {
            result = alipayClient.pageExecute(aliPayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String refund(String orderNo, String tradeNo, String amount, String reason, String refundRequestNo) {
        AlipayClient alipayClient = new DefaultAlipayClient(aliPayProperties.getGatewayUrl(), aliPayProperties.getAppid(),
                aliPayProperties.getAppPrivateKey(), aliPayProperties.getFormate(), aliPayProperties.getCharset(), aliPayProperties.getAlipayPublicKey(), aliPayProperties.getSignType());
        AlipayTradeRefundRequest aliPayRequest = new AlipayTradeRefundRequest();
        AlipayTradeRefundModel model=new AlipayTradeRefundModel();
        model.setOutTradeNo(orderNo);
        model.setTradeNo(tradeNo);
        model.setRefundAmount(amount);
        model.setRefundReason(reason);
        model.setOutRequestNo(refundRequestNo);
        aliPayRequest.setBizModel(model);

        //请求
        String result = null;
        try {
            result = alipayClient.pageExecute(aliPayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String refundQuery(String orderNo, String tradeNo, String refundRequestNo) {
        AlipayClient alipayClient = new DefaultAlipayClient(aliPayProperties.getGatewayUrl(), aliPayProperties.getAppid(),
                aliPayProperties.getAppPrivateKey(), aliPayProperties.getFormate(), aliPayProperties.getCharset(), aliPayProperties.getAlipayPublicKey(), aliPayProperties.getSignType());

        AlipayTradeFastpayRefundQueryRequest aliPayRequest = new AlipayTradeFastpayRefundQueryRequest();
        AlipayTradeFastpayRefundQueryModel model=new AlipayTradeFastpayRefundQueryModel();
        model.setOutTradeNo(orderNo);
        model.setTradeNo(tradeNo);
        model.setOutRequestNo(refundRequestNo);
        aliPayRequest.setBizModel(model);

        //请求
        String result = null;
        try {
            result = alipayClient.pageExecute(aliPayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String close(String orderNo, String tradeNo) {
        AlipayClient alipayClient = new DefaultAlipayClient(aliPayProperties.getGatewayUrl(), aliPayProperties.getAppid(),
                aliPayProperties.getAppPrivateKey(), aliPayProperties.getFormate(), aliPayProperties.getCharset(), aliPayProperties.getAlipayPublicKey(), aliPayProperties.getSignType());

        AlipayTradeCloseRequest aliPayRequest = new AlipayTradeCloseRequest();
        AlipayTradeCloseModel model =new AlipayTradeCloseModel();
        model.setOutTradeNo(orderNo);
        model.setTradeNo(tradeNo);
        aliPayRequest.setBizModel(model);

        //请求
        String result = null;
        try {
            result = alipayClient.pageExecute(aliPayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String downloadUrlQuery(String billType, String billDate) {
        AlipayClient alipayClient = new DefaultAlipayClient(aliPayProperties.getGatewayUrl(), aliPayProperties.getAppid(),
                aliPayProperties.getAppPrivateKey(), aliPayProperties.getFormate(), aliPayProperties.getCharset(), aliPayProperties.getAlipayPublicKey(), aliPayProperties.getSignType());
        AlipayDataDataserviceBillDownloadurlQueryRequest aliPayRequest = new AlipayDataDataserviceBillDownloadurlQueryRequest();
        AlipayDataDataserviceBillDownloadurlQueryModel model =new AlipayDataDataserviceBillDownloadurlQueryModel();
        model.setBillType(billType);
        model.setBillDate(billDate);
        aliPayRequest.setBizModel(model);
        //请求
        String result = null;
        try {
            result = alipayClient.execute(aliPayRequest).getBillDownloadUrl();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return result;
    }
}
